package com.dm.web.controller.base;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.dm.common.annotation.Log;
import com.dm.common.core.controller.BaseController;
import com.dm.common.core.domain.AjaxResult;
import com.dm.common.enums.BusinessType;
import com.dm.base.domain.BTask;
import com.dm.base.service.IBTaskService;
import com.dm.common.utils.poi.ExcelUtil;
import com.dm.common.core.page.TableDataInfo;

/**
 * 任务Controller
 *
 * @author dm
 * @date 2024-12-06
 */
@RestController
@RequestMapping("/base/task")
public class BTaskController extends BaseController {
    @Autowired
    private IBTaskService bTaskService;

    /**
     * 查询任务列表
     */
    @PreAuthorize("@ss.hasPermi('base:task:list')")
    @GetMapping("/list")
    public TableDataInfo list(BTask bTask) {
        startPage();
        List<BTask> list = bTaskService.selectBTaskList(bTask);
        return getDataTable(list);
    }

    /**
     * 导出任务列表
     */
    @PreAuthorize("@ss.hasPermi('base:task:export')")
    @Log(title = "任务", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BTask bTask) {
        List<BTask> list = bTaskService.selectBTaskList(bTask);
        ExcelUtil<BTask> util = new ExcelUtil<BTask>(BTask.class);
        util.exportExcel(response, list, "任务数据");
    }

    /**
     * 获取任务详细信息
     */
    @PreAuthorize("@ss.hasPermi('base:task:query')")
    @GetMapping(value = "/{taskId}")
    public AjaxResult getInfo(@PathVariable("taskId") Long taskId) {
        return success(bTaskService.selectBTaskByTaskId(taskId));
    }

    /**
     * 新增任务
     */
    @PreAuthorize("@ss.hasPermi('base:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody BTask bTask) {
        return toAjax(bTaskService.insertBTask(bTask));
    }

    /**
     * 修改任务
     */
    @PreAuthorize("@ss.hasPermi('base:task:edit')")
    @Log(title = "任务", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BTask bTask) {
        return toAjax(bTaskService.updateBTask(bTask));
    }

    /**
     * 删除任务
     */
    @PreAuthorize("@ss.hasPermi('base:task:remove')")
    @Log(title = "任务", businessType = BusinessType.DELETE)
    @DeleteMapping("/{taskIds}")
    public AjaxResult remove(@PathVariable Long[] taskIds) {
        return toAjax(bTaskService.deleteBTaskByTaskIds(taskIds));
    }

    /**
     * 执行任务
     */
    @PreAuthorize("@ss.hasPermi('base:task:do')")
    @Log(title = "任务", businessType = BusinessType.OTHER)
    @GetMapping("/doTask/{taskId}")
    public AjaxResult doTask(@PathVariable Long taskId) throws Exception {
        return toAjax(bTaskService.doTask(taskId));
    }

    /**
     * 执行语句
     */
    @PreAuthorize("@ss.hasPermi('base:task:add')")
    @Log(title = "任务", businessType = BusinessType.INSERT)
    @PostMapping("/doExecute")
    public AjaxResult doExecute(@RequestBody BTask bTask) {
        String s = bTaskService.doExecute(bTask);
        if (s.startsWith("[")) {
            return success(s);
        }
        return error(s);
    }
}
